const customRequst = require('../../../common/customRequst');
const customUser = require('../../../common/commonUser');
const app = getApp();

// 右侧每一类的 bar 的高度（固定）
const RIGHT_BAR_HEIGHT = 20;

// 右侧每个子类的高度（固定）
const RIGHT_ITEM_HEIGHT = 100;

// 左侧每个类的高度（固定）
const LEFT_ITEM_HEIGHT = 50

Page({
  data: {
    // 左 => 右联动 右scroll-into-view 所需的id
    HZL_toView: null,
    // 右侧每类数据到顶部的距离（用来与 右 => 左 联动时监听右侧滚动到顶部的距离比较）
    HZL_eachRightItemToTop: [],
    HZL_leftToTop: 0,
    // 当前左侧选择的
    HZL_currentLeftSelect: null,
    // 数据
    constants: [],
    periList: [],
    searchData: {
      cate_id: 'base',
      title: '',
      page: 1,
      pagesize: 15,
    },
    all_items: false,
    inputValue: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    // 页面创建时执行
    var that = this;
    that.getConstants();
    // 高度大小
    wx.getSystemInfo({
      success: function (res) {
        var HZL_height = res.windowHeight - 55
        var HZL_height1 = res.windowHeight - 110
        that.setData({
          HZL_height: HZL_height,
          HZL_height1: HZL_height1
        })
      }
    });
    that.setData({
      HZL_currentLeftSelect: null
    })
  },

  onShow: function (option) {
    //加载webscoket
    app.globalData.Chat.windowsobj = this;
    if (!app.globalData.Chat.serviceConfig.isWebScoket) {
      let userIsLogin = customUser.getUserToken();
      if (userIsLogin) {
        app.globalData.Chat.init();
      }
    }

    var that = this;
    //判断是否从其他地方进入
    let cate_id = app.globalData.globalCateId;
    if (cate_id != undefined && cate_id != null && cate_id != '') {
      let searchData = that.data.searchData;
      searchData.cate_id = cate_id;
      searchData.title = '';
      searchData.page = 1;
      searchData.pagesize = 15;
      that.setData({
        searchData: searchData,
        HZL_currentLeftSelect: cate_id
      });
      app.globalData.globalCateId = '';
    } else {
      let searchData = that.data.searchData;
      searchData.cate_id = 'base';
      searchData.title = '';
      searchData.page = 1;
      searchData.pagesize = 15;
      that.setData({
        searchData: searchData,
        HZL_currentLeftSelect: 'base'
      });
    }
    that.onloadPeriList()
  },

  /**
   * 左侧类的点击事件，
   * 点击时，右侧会滚动到对应分类
   */
  left: function (e) {
    let that = this;
    let cate_id = e.currentTarget.dataset.id;
    let seratchData = that.data.searchData;
    seratchData.cate_id = cate_id;
    seratchData.page = 1;
    this.setData({
      HZL_toView: e.target.id || e.target.dataset.id,
      HZL_currentLeftSelect: e.target.id || e.target.dataset.id,
      searchData: seratchData
    });
    this.onloadPeriList()
  },

  getConstants() {
    let that = this;
    customRequst.postRequest('api/cate/list', ).then(res => {
      if(res.code == 1){
        that.setData({
          constants: res.data,
          HZL_currentLeftSelect: res.data[0].id,
        })
      }
    })
  },

  //书刊列表数据
  onloadPeriList() {
    let that = this;
    customRequst.postRequest('api/peri/list', that.data.searchData).then(res => {
      if (res.code == 1) {
        let perilist = that.data.periList;
        if (res.data != '') {
          if (that.data.searchData.page != 1) {
            for (let i in res.data) {
              perilist.push(res.data[i])
            }
          } else {
            perilist = res.data;
          }
        } else {
          if (that.data.searchData.page == 1) {
            perilist = [];
          }
        }
        that.setData({
          periList: perilist
        });
        if (perilist == '' || res.data.length < that.data.searchData.pagesize) {
          //显示加载全部数据
          that.setData({
            all_items: true
          });
        }
      } else {
        wx.showToast({
          title: res.msg,
        })
      }
    })
  },
  searchList: function (e) {
    let that = this;
    let title = e.detail;
    let searchData = that.data.searchData;
    searchData.title = title;
    searchData.page = 1;
    if (title) {
      that.setData({
        searchData: searchData
      });
      that.onloadPeriList();
    } else {
      that.onloadPeriList();
    }
  },
  onPeriItem(event) {
    let id = event.currentTarget.dataset.id;
    let url = '/pages/peris/detail/index?id=' + id;
    wx.navigateTo({
      url: url
    });
  },

  //滚动=条触底
  scrollSearchList: function () {
    let that = this;
    if (that.data.all_items) {
      return;
    }
    let searchData = that.data.searchData;
    searchData.page = searchData.page + 1;
    that.setData({
      searchData: searchData
    });
    that.onloadPeriList();
  },
  handleNewMessage: function (msg) {
    wx.showTabBarRedDot({
      index: 2,
    })
  },
  //处理聊天返回的数据
  handleChatMag: function (msg) {
    switch (msg.msgtype) {
      case 'new_message': //收到新消息
        wx.showTabBarRedDot({
          index: 2,
        });
        break;
      case "push_sys_message":
        wx.showTabBarRedDot({
          index: 2,
        });
        break;
    }
  },

})